package org.hackystat.devcathlon.ui.page.profile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.hackystat.devcathlon.engine.profile.Profile;
import org.hackystat.devcathlon.engine.test.DevcathlonTestHelper;
import org.junit.Test;

/**
 * Tests Profile class.
 *
 * @author Anthony Du, John Ancheta
 */
public class TestProfile extends DevcathlonTestHelper {
  /**
   * Test Profile instance.
   */
  @Test
  public void testProfileInstance() {
    Profile defaultProfile = new Profile();
    defaultProfile.getAvatar();
    Profile profileInstance = new Profile("screenName", "firstname", "lastname", "mybioinfo");
    assertNotNull("should not be null", defaultProfile);
    assertNotNull("should not be null", profileInstance);
    testProfileInstance(defaultProfile);
    testProfileInstance(profileInstance);
  }
  
  /**
   * Test any given profile instance.
   * @param profileInstance A Profile
   */
  private void testProfileInstance(Profile profileInstance) {
    String screename = profileInstance.getScreenName();
    String firstname = profileInstance.getFirstName();
    String lastname = profileInstance.getLastName();
    String fullname = profileInstance.getFullName();
    String bioInfo = profileInstance.getBioInfo();
    String gender = profileInstance.getGender();
    String location = profileInstance.getLocation();
    String contact = profileInstance.getContact();
    String avatar = profileInstance.getAvatar();
    profileInstance.setScreenName(screename);
    profileInstance.setFirstName(firstname);
    profileInstance.setLastName(lastname);
    profileInstance.setBioInfo(bioInfo);
    profileInstance.setGender(gender);
    profileInstance.setLocation(location);
    profileInstance.setContact(contact);
    profileInstance.setAvatar(avatar);
    assertEquals("screename should be equal", screename, profileInstance.getScreenName());
    assertEquals("firstname should be equal", firstname, profileInstance.getFirstName());
    assertEquals("lastname should be equal", lastname, profileInstance.getLastName());
    assertEquals("fullname should be equal", fullname, profileInstance.getFullName());
    assertEquals("bioinfo should be equal", bioInfo, profileInstance.getBioInfo());
    profileInstance.setBioInfo("");
    assertNotNull("should not be empty", profileInstance.getBioInfo());
    assertEquals("gender should be equal", gender, profileInstance.getGender());
    assertEquals("location should be equal", location, profileInstance.getLocation());
    assertEquals("contact should be equal", contact, profileInstance.getContact());
    /* Test full name composite */
    assertEquals("fullname should be equal", firstname + " " + lastname, fullname);
  }
  
  /**
   * Test default screen name composition.
   */
  @Test
  public void testGetScreenName() {
    Profile profileInstance = new Profile("john.doe@gmail.com");
    assertEquals("screen name should be equal", "john.doe", profileInstance.getScreenName());
    Profile profileInstance2 = new Profile("johndoegmail.com");
    assertEquals("screen name should be empty", "", profileInstance2.getScreenName());
  }
  
  /**
   * Test adding and removing email addresses.
   */
  @Test
  public void testEmailAddresses() {
    Profile profileInstance = new Profile("john.doe@gmail.com");
    assertEquals("has one email address", true,
            profileInstance.getEmailAddresses().contains("john.doe@gmail.com"));
    /* Test adding an email address */
    profileInstance.addEmail("johndoe.com"); // invalid email
    assertEquals("has 1 email address", 1, profileInstance.getEmailAddresses().size());
    profileInstance.addEmail("average.joe@gmail.com");
    assertEquals("has email address", true,
            profileInstance.getEmailAddresses().contains("average.joe@gmail.com"));
    assertEquals("has 2 email addresses", 2, profileInstance.getEmailAddresses().size());
  }
}
